package com.healthyeating.manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONUtil;
import com.healthyeating.common.exception.ServiceException;
import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.manage.domain.Drink;
import com.healthyeating.manage.domain.Eat;
import com.healthyeating.manage.domain.vo.AssocNutritionVo;
import com.healthyeating.manage.mapper.DrinkMapper;
import com.healthyeating.manage.mapper.EatMapper;
import com.healthyeating.manage.mapper.NutritionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.AssocNutritionMapper;
import com.healthyeating.manage.domain.AssocNutrition;
import com.healthyeating.manage.service.IAssocNutritionService;

/**
 * 饮食和营养关联Service业务层处理
 *
 * @author 段奇乐
 * @date 2024-12-30
 */
@Service
public class AssocNutritionServiceImpl implements IAssocNutritionService {

    @Autowired
    private AssocNutritionMapper assocNutritionMapper;

    @Autowired
    private EatMapper eatMapper;

    @Autowired
    private DrinkMapper drinkMapper;

    @Autowired
    private NutritionMapper nutritionMapper;

    private String exchangeNames(String nutritionIds) {
        JSONArray _nutritionIds = JSONUtil.parseArray(nutritionIds);
        StringBuilder builder = new StringBuilder();
        Long[] _nutritionIdList = _nutritionIds.toArray(new Long[0]);
        for (int i = 0; i <_nutritionIdList.length; i++) {
            builder.append(nutritionMapper.selectNutritionByNutritionId(_nutritionIdList[i]).getNutritionName());
            if (i != _nutritionIdList.length - 1) {
                builder.append("，");
            }
        }
        return builder.toString();
    }

    /**
     * 查询饮食和营养关联
     *
     * @param id 饮食和营养关联主键
     * @return 饮食和营养关联
     */
    @Override
    public AssocNutrition selectAssocNutritionById(Long id) {
        return assocNutritionMapper.selectAssocNutritionById(id);
    }

    /**
     * 查询饮食和营养关联列表
     *
     * @param assocNutrition 饮食和营养关联
     * @return 饮食和营养关联
     */
    @Override
    public Map<String ,Object> selectAssocNutritionList(AssocNutrition assocNutrition) {
        Map<String, Object> map = new HashMap<>();
        List<AssocNutrition> assocNutritions = assocNutritionMapper.selectAssocNutritionList(assocNutrition);
        List<AssocNutritionVo> assocNutritionVos = assocNutritions.stream().map(assoc -> {
            // 根据source查询不同的饮食对象
            String source = assoc.getSource();
            AssocNutritionVo assocNutritionVo = new AssocNutritionVo();
            if (source.equals("tb_eat")) {
                // 为assoc新增属性
                BeanUtil.copyProperties(assoc, assocNutritionVo);
                Eat eat = eatMapper.selectEatByEatId(assoc.getAssocId());
                assocNutritionVo.setDiet(eat);
                // 将nutritionIds转换为名称，将其转化为A，B格式存到nutritionNames(String)中
                String nutritionIds = assoc.getNutritionIds();
                if (nutritionIds != null && !nutritionIds.isEmpty()) {
                    String _nutritionNames = exchangeNames(nutritionIds);
                    assocNutritionVo.setNutritionNames(_nutritionNames);
                }
            } else if (source.equals("tb_drink")) {
                // 为assoc新增属性
                BeanUtil.copyProperties(assoc, assocNutritionVo);
                Drink drink = drinkMapper.selectDrinkByWaterId(assoc.getAssocId());
                assocNutritionVo.setDiet(drink);
                // 将nutritionIds转换为名称，将其转化为A，B格式存到nutritionNames(String)中
                String nutritionIds = assoc.getNutritionIds();
                if (nutritionIds != null && !nutritionIds.isEmpty()) {
                    String _nutritionNames = exchangeNames(nutritionIds);
                    assocNutritionVo.setNutritionNames(_nutritionNames);
                }
            }
            return assocNutritionVo;
        }).collect(Collectors.toList());
        map.put("oldList", assocNutritions);
        map.put("newList", assocNutritionVos);
        return map;
    }

    /**
     * 新增饮食和营养关联
     *
     * @param assocNutritionList 饮食和营养关联集合
     * @return 结果
     */
    @Override
    public int insertAssocNutrition(List<AssocNutrition> assocNutritionList) {
        List<AssocNutrition> insertList = assocNutritionList.stream().map(assocNutrition -> {
            List<AssocNutrition> assocNutritions = assocNutritionMapper.selectAssocNutritionList(assocNutrition);
            if (!assocNutritions.isEmpty()) {
                throw new ServiceException("该饮食名称已存在，请更换！");
            } else {
                assocNutrition.setCreateTime(DateUtils.getNowDate());
                assocNutrition.setUpdateTime(DateUtils.getNowDate());
                assocNutrition.setUpdateBy(SecurityUtils.getUsername());
            }
            return assocNutrition;
        }).collect(Collectors.toList());
        return assocNutritionMapper.batchInsertAssocNutrition(insertList);
    }

    /**
     * 修改饮食和营养关联
     *
     * @param assocNutrition 饮食和营养关联
     * @return 结果
     */
    @Override
    public int updateAssocNutrition(AssocNutrition assocNutrition) {
        assocNutrition.setUpdateBy(SecurityUtils.getUsername());
        assocNutrition.setUpdateTime(DateUtils.getNowDate());
        return assocNutritionMapper.updateAssocNutrition(assocNutrition);
    }

    /**
     * 批量删除饮食和营养关联
     *
     * @param ids 需要删除的饮食和营养关联主键
     * @return 结果
     */
    @Override
    public int deleteAssocNutritionByIds(Long[] ids) {
        return assocNutritionMapper.deleteAssocNutritionByIds(ids);
    }

    /**
     * 删除饮食和营养关联信息
     *
     * @param id 饮食和营养关联主键
     * @return 结果
     */
    @Override
    public int deleteAssocNutritionById(Long id) {
        return assocNutritionMapper.deleteAssocNutritionById(id);
    }
}
